﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Demo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            cookiesGrid.DataSource = GetCookies();
            cookiesGrid.DataBind();

            refresh.Click += (o, z) =>
            {
                Session["Text"] = sessionText.Text;
            };

            bakeCookie.Click += (o, z) =>
            {
                Session["Text"] = sessionText.Text;
                var newCookie = new HttpCookie(cookieName.Text, cookieValue.Text);
                newCookie.Expires = DateTime.Now.AddMinutes(double.Parse(cookieExpiresInMinutes.Text));
                Response.Cookies.Add(newCookie);
            };
            redirect1.Click += (o, z) =>
            {
                Response.Redirect("~/Default.aspx");
            };
            redirect2.Click += (o, z) =>
            {
                Response.Redirect("~/Default.aspx", true);
            };
            throwException.Click += (o, z) => { throw new NotImplementedException("Booh"); };
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            sessionText.Text = Session["Text"] as string;
        }

        private IEnumerable<object> GetCookies()
        {
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                var cookie = HttpContext.Current.Request.Cookies[i];
                yield return new
                {
                    Name = cookie.Name,
                    Expires = cookie.Expires,
                    Value = cookie.Value
                };
            }
        }
    }
}
